// 数组的栈方法 push()、pop()
// 括号匹配检验
var express = "(x*(x+y-A[2*i+1]+1)-5+123"
var newArr = express.split("")
var empty = []
var transArr;
function stack(newArr){
    for(var i = 0; i < newArr.length; i++){
        switch(newArr[i]){
            case "(":
            empty.push("(");
            break;
            case "{":
            empty.push("{");
            break;
            case "[":
            empty.push("[");
            break;
            case ")":
            transArr = empty.pop();
            if(transArr != "("){
                console.log("括号不匹配")
                return false;
            }
            break;
            case "}":
            transArr = empty.pop();
            if(transArr != "{"){
                console.log("括号不匹配")
                return false;
            }
            break;
            case "]":
            transArr = empty.pop();
            if(transArr != "["){
                console.log("括号不匹配")
                return false;
            }
            break;
        }
        
    }
    
    if(empty.length == 0){
        console.log("括号匹配")
    }else{
        console.log("括号不匹配")
    }
}
stack(newArr)


// 计算表达式

var operator = [];
var operand = [];
var str = "1*2+3*4-3/6";
for(var i=0; i<str.length; i++){
    switch (str[i]){
        case '(':{
            operator.push(str[i])
            break;
        }
        case ')':{
            if (operator.length == 0) {
                console.log("括号不匹配")
            }
            temp=operator.pop();//非空，弹出对象
            while(temp!='('){//只要不是左括号，则全部弹出
                operand.push(temp);//并输出到后缀表达式中
                if(operator.length==0){//保证栈为空
                    break;
                }
                temp=operator.pop();
            }
            break;
        }
        case '*':
        case '/':{
            if(operator.length == 0){
                operator.push(str[i])
            }else{
                temp = operator[operator.length-1];
                if(temp == "+" || temp == "-"){
                    operator.push(str[i])
                }
            }
            break;
        }
        case '+':
        case '-':{
            if(operator.length == 0){
                operator.push(str[i])
            }else{
                temp = operator[operator.length-1];
                if(temp == "*" || temp == "/"){
                    if(operator.length != 0) {
                        temp = operator.pop();//栈中的出栈
                        operand.push(temp);//保存到存储空间
                    }
                    operator.push(str[i])
                }
            }
            break;
        }
        default:
        {
            operand.push(str[i]);
            break;
        }
    }
}
//最后将栈中剩下的操作符输出
while(operator.length!=0){
    temp=operator.pop();
    operand.push(temp);
}
console.log(operand)
function computMethod(operand){
    var newArr = []
    for(var i=0;i<operand.length;i++){
        switch (operand[i]){
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
            {
                newArr.push(operand[i]);
                break;
            }
            case '+':{//遇到操作符则出栈两个元素进行对应操作
                temp=newArr.pop()+newArr.pop();
                newArr.push(temp);//再将结果入栈
                temp=null;
                break;
            }
            case '-':{
                s=newArr.pop();
                temp=newArr.pop()-s;
                newArr.push(temp);
                s=null;temp=null;
                break;
            }
            case '*':{
                temp=newArr.pop()*newArr.pop();
                newArr.push(temp);//再将结果入栈
                temp=null;
                break;
            }
            case '/':{
                s=newArr.pop();
                temp=newArr.pop()/s;
                newArr.push(temp);
                s=null;temp=null;
                break;
            }

        }

    }
    return newArr.pop();//算出结果
}
console.log(computMethod(operand))
